package codegen

import (
	"gitee.com/hxchjm/goge/codegen/utils"
	"gitee.com/hxchjm/goge/codegen/utils/pathx"
	"github.com/siddontang/go/config"
	"strings"
)

const modTemplate = `module {{.serviceName}}

go 1.19

require (
	github.com/gogo/protobuf v1.3.2
	github.com/golang/protobuf v1.5.2
	go-main v0.0.0
	google.golang.org/genproto v0.0.0-20220722212130-b98a9ff5e252
	google.golang.org/grpc v1.48.0
)

replace go-main => gitee.com/hxchjm/go-main v0.0.0-20240208143825-818e387a63f0
`

func genMod(dir, rootPkg string, cfg *config.Config, filename string) error {
	if ss := strings.Split(rootPkg, "/"); len(ss) > 0 {
		if pathx.FileExists(pathx.JoinPackages("..", ss[0], "go.mod")) { //workdir目录下存在go.mod，不需要创建
			return nil
		}
	}
	if !pathx.FileExists("go.mod") { //当前项目下存在go.mod，也不创建
		return nil
	}
	return utils.GenFile(utils.FileGenConfig{
		Dir:             dir,
		Subdir:          "",
		Filename:        filename,
		BuiltinTemplate: modTemplate,
		Data: map[string]string{
			"serviceName": rootPkg,
		},
	})
}
